//-----------------------------------------------------------------------------
//  Copyright (C) 2005-2008 Thomas S. Ullrich 
//
//  This file is part of the Solitaire Mahjong project.
//
//  This file may be used under the terms of the GNU General Public License.
//  This project is free software; you can redistribute it and/or modify it
//  under the terms of the GNU General Public License.
//  
//  Author: Thomas S. Ullrich
//  Last update: June 1, 2007
//-----------------------------------------------------------------------------
#include <iostream>
#include <QApplication>
#include <QMouseEvent>
#include <QMessageBox>
#include <QtDebug>
#include "smBoardView.h"
#include "smTileItem.h"
#include "smMainWindow.h"
#include "smBoardView.h"

using namespace std;
 
smBoardView::smBoardView(QWidget *parent) : QGraphicsView(parent)
{
    mScene = new QGraphicsScene(this);
    //setFrameShadow(QFrame::Plain);
    setFrameStyle(QFrame::NoFrame);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setScene(mScene);
    // setCacheMode(QGraphicsView::CacheBackground);
    // setOptimizationFlag (QGraphicsView::DontSavePainterState, true );
    setViewportUpdateMode(SmartViewportUpdate);
}

smBoardView::~smBoardView() { /* noop */ }

QGraphicsScene* smBoardView::scene() {return mScene;}
 
void smBoardView::mousePressEvent(QMouseEvent* e)
{
    smMainWindow* win = dynamic_cast<smMainWindow*>(parent());
    if (!win) {
        QMessageBox::critical( 0, "Mahjong Solitaire",
                               "Internal error in smBoardView::contentsMousePressEvent()\n"
                               "occured (win == 0).\n"
                               "The application will now exit.");
        exit(1);
    }
    
    if (e->button() == Qt::RightButton)
        win->itemsSelectedForRemoval();
    
    QPointF p = e->pos();
    QList<QGraphicsItem*> l = scene()->items(p);
    
    smTileItem *topItem = 0;
    for (QList<QGraphicsItem*>::Iterator it=l.begin(); it!=l.end(); ++it) {
        smTileItem *item = dynamic_cast<smTileItem*>(*it);
        if (item) {
            if (topItem) {
                if (topItem->layer() < item->layer()) topItem = item;
            }
            else
                topItem = item;
        }
    }
    if (e->button() == Qt::LeftButton) win->itemSelected(topItem);
}

void smBoardView::mouseDoubleClickEvent(QMouseEvent* e)
{
    smMainWindow* win = dynamic_cast<smMainWindow*>(parent()); 
    if (!win) {
        QMessageBox::critical( 0, "Mahjong Solitaire",
                               "Internal error in smBoardView::contentsMouseDoubleClickEvent()\n"
                               "occured (win == 0).\n"
                               "The application will now exit.");
        exit(1);
    }
    
    QPointF p = e->pos();
    QList<QGraphicsItem*> l = scene()->items(p);
    
    smTileItem *topItem = 0;
    for (QList<QGraphicsItem*>::Iterator it=l.begin(); it!=l.end(); ++it) {
        smTileItem *item = dynamic_cast<smTileItem*>(*it);
        if (item) {
            if (topItem) {
                if (topItem->layer() < item->layer()) topItem = item;
            }
            else
                topItem = item;
        }
    }
    if (win->numberOfItemsSelected() == 1)
        win->itemSelected(topItem);
    //
    // Remove item but only when selected item is
    // double clicked, ie don't remove a pair when
    // the mouse is not over one of them.
    // Right click still works (see above method).
    //
    if (topItem && topItem->highlighted())
        win->itemsSelectedForRemoval();
}
